package com.example.mybatis.session.impl;

import com.example.mybatis.config.Config;
import com.example.mybatis.executor.impl.SimpleExecutor;
import com.example.mybatis.plugin.Interceptor;
import com.example.mybatis.plugin.InterceptorChain;
import com.example.mybatis.session.SqlSessionFactory;
import com.example.mybatis.sqlsession.SqlSession;

import com.example.mybatis.executor.Executor ;

import java.util.List;

public class DefaultSessionFactory  implements SqlSessionFactory {

    private final Config config;

    public DefaultSessionFactory(Config config) {
        this.config = config;
    }

    @Override
    public SqlSession openSession() {
        Executor executor = new SimpleExecutor();
        //插件
        InterceptorChain interceptorChain = new InterceptorChain();
        List<Interceptor>  interceptors =   config.getInterceptorList();
        interceptorChain.add(interceptors);
        executor = (Executor) interceptorChain.pluginAll(executor);
        //创建
        return  new DefaultSqlSession(config,executor);
      }

    @Override
    public Config getConfig() {
        return config;
    }
}
